package com.rhb.af.web.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.rhb.af.web.bean.Menu;

public class HtmlMenu {
	public static String getSource(Map<String, Menu> menus) {
		StringBuffer buf = new StringBuffer(1000);

		buf.append("<script type='text/javascript'>");
		buf.append("function menuFix() {");
		buf.append("	var sfEls=document.getElementById('nav').getElementsByTagName('li');");
		buf.append("	for(var i=0; i<sfEls.length; i++){");
		buf.append("		sfEls[i].onmouseover=function(){this.className='sfhover';};");
		buf.append("        sfEls[i].onmouseout=function(){this.className='';};");
		buf.append("	}");
		buf.append("}");
		buf.append(" window.onload=menuFix;");
		buf.append("</script> \n");

		buf.append("<ul id='nav'>\n");
		Iterator<String> ite = menus.keySet().iterator();
		HashMap<String,List> catalogs = new HashMap();
		while (ite.hasNext()) {
			Menu menu = menus.get(ite.next());
			if(catalogs.containsKey(menu.getCatalog())){
				(catalogs.get(menu.getCatalog())).add(menu);
			}else{
				ArrayList l = new ArrayList();
				l.add(menu);
				catalogs.put(menu.getCatalog(), l);
			}
		}
		
	
		Iterator<String> i = catalogs.keySet().iterator();
		while(i.hasNext()){
			String catalog = i.next();
			buf.append("<li>");
			buf.append("<a href='#'>");
			buf.append(catalog);
			buf.append("</a>");
			buf.append("<ul>");
			List<Menu> list = catalogs.get(catalog);
			for(int j=0; j<list.size(); j++){
				Menu menu = list.get(j);
				buf.append("<li>");
				buf.append("<a href='"+menu.getHref()+"'>");
				buf.append(menu.getCaption());
				buf.append("</a>");
				buf.append("</li>");
			}
			buf.append("</ul>");
			buf.append("</li>");
		}
		buf.append("</ul>");
		return buf.toString();
	}
}
